package com.c1202m.util;

import com.c1202m.gui.ATMSystem;
import java.util.regex.Pattern;
import javax.swing.RowFilter;
import org.jdesktop.beans.AbstractBean;
import org.jdesktop.swingx.JXTable;

public class MyTableFilter extends AbstractBean {

    private RowFilter<Object, Object> searchFilter;
    private String filterString;
    private JXTable table;

    public MyTableFilter(JXTable table) {
        this.table = table;
    }

    public boolean isFilteringByString() {
        return !isEmpty(getFilterString());
    }

    private boolean isEmpty(String filterString) {
        return filterString == null || filterString.length() == 0;
    }

    public void setFilterString(String filterString) {
        String oldValue = getFilterString();
        this.filterString = filterString;
        updateSearchFilter();
        firePropertyChange("filterString", oldValue, getFilterString());
    }

    public String getFilterString() {
        return filterString;
    }

    private void updateSearchFilter() {
        if ((filterString != null) && (filterString.length() > 0)) {
            searchFilter = createSearchFilter(".*" + filterString + ".*");
        } else {
            searchFilter = null;
        }
        updateFilters();
    }

    private void updateFilters() {
        table.setRowFilter(searchFilter);
    }

    private RowFilter<Object, Object> createSearchFilter(final String filterString) {
        return new RowFilter<Object, Object>() {
            @Override
            public boolean include(Entry<? extends Object, ? extends Object> entry) {
                Pattern p = Pattern.compile(".*" + filterString + ".*", Pattern.CASE_INSENSITIVE);
                String value1 = entry.getValue(0).toString();
                String value2 = entry.getValue(1).toString();


                boolean matches1 = false;
                boolean matches2 = false;


                matches1 = p.matcher(value1).matches();
                matches2 = p.matcher(value2).matches();


                if (table.getName() != null && table.getName().equals("bangQuanTri")) {
                    String value3 = entry.getValue(2).toString();
                    boolean matches3 = false;
                    matches3 = p.matcher(value3).matches();
                    return matches2 || matches3;
                }

                if (table.getName() != null && table.getName().equals("bangTaiKhoan")) {
                    return matches1 || matches2;
                }

                if (table.getName() != null && table.getName().equals("bangDanhSachTK")) {
                    return matches1 || matches2;
                }
                
                if (table.getName() != null && table.getName().equals("bangTKRutTien")) {
                    String value4 = entry.getValue(3).toString();
                    boolean matches4 = false;
                    matches4 = p.matcher(value4).matches();
                    return matches2 || matches4;
                }
                
                if (table.getName() != null && table.getName().equals("bangTKGuiTien")) {
                    String value4 = entry.getValue(3).toString();
                    boolean matches4 = false;
                    matches4 = p.matcher(value4).matches();
                    return matches2 || matches4;
                }
                
                if (table.getName() != null && table.getName().equals("bangTKChuyenKhoan")) {
                    String value3 = entry.getValue(2).toString();
                    String value4 = entry.getValue(3).toString();
                    String value5 = entry.getValue(4).toString();
                    boolean matches3 = false;
                    boolean matches4 = false;
                    boolean matches5 = false;
                    matches3 = p.matcher(value3).matches();
                    matches4 = p.matcher(value4).matches();
                    matches5 = p.matcher(value5).matches();
                    return matches2 || matches3 || matches4 || matches5;
                }

                if (ATMSystem.kichHoatTaiKhoan) {
                    String value3 = entry.getValue(2).toString();
                    boolean matches3 = false;
                    matches3 = p.matcher(value3).matches();
                    return matches1 || matches2 || matches3;
                }

                return matches1 || matches2;
            }
        };
    }
}
